


const {
    dictData_controller_save,
    dictData_controller_find,
    dictData_controller_get,
    dictData_controller_delete,
} = require("../../../controller/system/dict/DICT.DATA.CONTROLLER");
const { emptyCheck } = require("../../../utils/checkEmpty");
const pageSizeList = require("../../../utils/slicePageSize");
const { serverError } = require("../../../utils");
/**
 * @description 字典详情路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/system/dict/data");

/**@description 新建|修改路由 */
router.post("/create", async (ctx, next) => {
    try {
        console.log("发过来的参数body", ctx.request.body)
        const {        // shopId,
            id = undefined,
            dictType = undefined,
            label = undefined,
            value = undefined,
            sort = undefined,
            colorType = undefined,
            cssClass = undefined,
            remark = undefined,
            status = undefined,
        } = ctx.request.body
        const check = emptyCheck({ dictType, label, value })
        if (check.code !== 0) {
            ctx.body = check
        } else {
            const res = await dictData_controller_save({
                id,
                dictType,
                label,
                value,
                sort,
                colorType,
                cssClass,
                remark,
                status,
            })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})


/**@description 删除路由 根据id进行删除 */
router.delete("/delete", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await dictData_controller_delete({ ids: [id] });
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})

/**@description 列表查询路由 */
router.post("/list", async (ctx, next) => {
    try {
        const { dictType, pageSize, current } = ctx.request.body;
        const check = emptyCheck({ dictType })
        if (check.code !== 0) {
            ctx.body = check
            return;
        }
        const res = await dictData_controller_find({
            dictType, pageSize, current
        })
        ctx.body = res
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }

})

/**@description 字典详情数据列表查询 @param {dictType} 字典类型参数 */
router.get("/get", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await dictData_controller_get({ id });

        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})
module.exports = router